#include "stdafx.h"
#include "DiagramImage.h"

#include <QDebug>
#include <QPainter>
#include <QApplication>
#include <QGraphicsScene>

DiagramImage::DiagramImage(QObject *parent)
{
	setFlag(QGraphicsItem::ItemSendsGeometryChanges,true);
	//setFlag(QGraphicsItem::ItemIsMovable,true);
	loadImageData(QString(":/imgview/picture"));
}

DiagramImage::~DiagramImage()
{

}

bool DiagramImage::loadImageData( QString strFilePath )
{
	return m_pxImg.load(strFilePath);
}

bool DiagramImage::loadImageData( const QByteArray & data )
{
	return m_pxImg.loadFromData(data);
}

void DiagramImage::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget /*= 0 */ )
{
	if(!m_pxImg.isNull())
	{
		//QRectF rtDisplay;
		//getMatchDiaplaySize(rtDisplay);
		//setGeometry(rtDisplay);
		//QImage img = m_pxImg.scaled(rtDisplay.size().toSize(),Qt::KeepAspectRatio);
		//painter->drawImage(0,0,img);
		painter->drawImage(0,0,m_pxImg);
	}
	return QGraphicsWidget::paint(painter,option,widget);
}


void DiagramImage::getMatchDiaplaySize( QRectF& rtDisplay )
{
	int nImgW = m_pxImg.width();
	int nImgH = m_pxImg.height();

	int nScreenW = m_rtScreen.width();
	int nScreenH = m_rtScreen.height();

	if(nImgW * nImgH <= 0)  return;
	if(nScreenW * nScreenH <= 0)  return;

	float  fScaleImg = nImgW / (nImgH * 1.0f);
	
	if(fScaleImg >= 1.0f)
	{
		if(nScreenW >= nImgW)
		{
			float fCenterPosX = qAbs((nScreenW - nImgW) / 2.0f);
			float fCenterPosY = qAbs((nScreenH - nImgH) / 2.0f);
			
			rtDisplay |= QRectF(fCenterPosX,fCenterPosY,nImgW,nImgH);
		}
		else
		{
			float fScaleH = nScreenW * nImgH / (nImgW * 1.0f);

			if(fScaleH >= nScreenH)
			{
				float fScaleW = nScreenH * nImgW / (nImgH * 1.0f);
				float fCenterPosX = qAbs((nScreenW - fScaleW) / 2.0f);
				float fCenterPosY = 0;
				rtDisplay |= QRectF(fCenterPosX,fCenterPosY,fScaleW,nScreenH);
			}
			else
			{
				float fCenterPosX = 0;
				float fCenterPosY = qAbs((nScreenH - fScaleH) / 2.0f);
				rtDisplay |= QRectF(fCenterPosX,fCenterPosY,nScreenW,fScaleH);
			}
			
		}
	}

	else
	{
		if(nScreenH >= nImgH)
		{
			float fCenterPosX = qAbs((nScreenW - nImgW) / 2.0f);
			float fCenterPosY = qAbs((nScreenH - nImgH) / 2.0f);

			rtDisplay |= QRectF(fCenterPosX,fCenterPosY,nImgW,nImgH);
		}
		else
		{
			
			float fScaleW = nScreenH * nImgW / (nImgH * 1.0f);
			if(fScaleW >= nScreenW)
			{
				float fScaleH = nScreenW * nImgH / (nImgW * 1.0f);
				float fCenterPosX = 0;
				float fCenterPosY = qAbs((nScreenH - fScaleH) / 2.0f);
				rtDisplay |= QRectF(fCenterPosX,fCenterPosY,fScaleW,nScreenH);
			}
			else
			{
				float fCenterPosX = qAbs((nScreenW - fScaleW) / 2.0f);
				float fCenterPosY = 0;
				rtDisplay |= QRectF(fCenterPosX,fCenterPosY,fScaleW,nScreenH);
			}
		}

	}

}

QSize DiagramImage::getImageSize()
{
	return m_pxImg.size();
}

void DiagramImage::setImageTransform( QRectF &rtDisplay )
{
	resetTransform();
	QTransform  transf;
	float fScaleX = rtDisplay.width() / (m_pxImg.width() * 1.0f)  ;
	float fScaleY = rtDisplay.height() /( m_pxImg.height() * 1.0f);
	transf.scale(fScaleX,fScaleY);
	setTransform(transf);
	setPos(rtDisplay.x(),rtDisplay.y());
}

bool DiagramImage::event(QEvent * e )
{
	if(e->type() == WM_NOTIFY_WND)
	{
		DiagramItemEvent* gve = static_cast<DiagramItemEvent* >(e);

		if(gve->msg.message == DK_COMMAND_RESIZEVIEW)	
		{
			QRect* pRect = (QRect*)gve->msg.lParam;
			m_rtScreen = *pRect;

			setGeometry(0,0,m_pxImg.width(),m_pxImg.height());
		}

	}

	return QGraphicsWidget::event(e);
}
